package com.zzq.calcgame;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.zzq.calcgame.entity.Papers;
import com.zzq.calcgame.model.ExamManager;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class RecordActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        setTitle("答题记录列表");
        listView= (ListView) findViewById(R.id.listview);

        final List<Papers> paperses= ExamManager.getInstance(this).getAllPaper();

        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return paperses.size();
            }

            @Override
            public Papers getItem(int i) {
                return paperses.get(i);
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                ViewHolder viewHolder;
                if(view==null){
                    viewHolder=new ViewHolder();
                    view=View.inflate(RecordActivity.this,R.layout.item_layout_records,null);
                    viewHolder.title= (TextView) view.findViewById(R.id.title);
                    viewHolder.content= (TextView) view.findViewById(R.id.content);
                    viewHolder.count= (TextView) view.findViewById(R.id.count);
                    view.setTag(viewHolder);
                }else{
                    viewHolder= (ViewHolder) view.getTag();
                }

                Papers item=getItem(i);
                viewHolder.title.setText(SimpleDateFormat.getDateInstance().format(new Date(item.getCreateTime())));
                viewHolder.content.setText(String.format(Locale.getDefault(),"正确%s 错误%s",item.getTotalScore(),item.getWrongCount()));

                viewHolder.count.setText("总数"+item.getQuestionCount());
                return view;
            }
        });


        listView.setOnItemClickListener(this);
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Papers item= (Papers) listView.getAdapter().getItem(i);
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("MODE",2);
        intent.putExtra("paperId",item.getId());
        startActivity(intent);
    }

    static class ViewHolder{
        TextView title,content,count;
    }
}
